import tr
from flask import Flask
from flask import request
import json
import io
import os
import random

app = Flask(__name__)
file_path ='./img'
if not os.path.exists(file_path):
    os.mkdir(file_path)

img_path = './img/{}.png'


@app.route('/')
def index():
    return 'home_page, use ocr at: http://127.0.0.1:12345/ocr and method is post'


@app.route('/ocr', methods=["POST"])
def ocr():
    upload_file = request.files['file']
    save_path = img_path.format(str(random.random()).replace('.', ''))
    upload_file.save(save_path)
    result = tr.run('./ocr.png')
    data = {
        'result': result
    }
    os.remove(save_path)
    return json.dumps(data)


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=12345, processes=10)



